package org.example.testcase;

import com.microsoft.playwright.*;
import com.microsoft.playwright.options.*;
import io.qameta.allure.Description;
import org.testng.ITestContext;
import org.testng.annotations.*;

import java.nio.file.Paths;
import java.util.Random;
import java.util.regex.Pattern;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

public class taskCenter {

    Playwright playwright;
    Browser browser;
    BrowserContext context;
    Page page;

    Random random = new Random();
    int randomNumber = random.nextInt(100) + 1;
    String projectTaskName = "ui自动化测试"+randomNumber;

    private static final String STORAGE_STATE_PATH = "auth.json";

    @BeforeClass
    public void setUp() {
        playwright = Playwright.create();
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(true).setSlowMo(50));

        context = browser.newContext(new Browser.NewContextOptions().setStorageStatePath(Paths.get(STORAGE_STATE_PATH)));
        page = context.newPage();
    }

    @AfterClass
    public void tearDown() {
        if (browser != null) {
            browser.close();
        }
        if (playwright != null) {
            playwright.close();
        }
    }

    @BeforeMethod
    public void setUpTest(ITestContext context) {
        context.setAttribute("page", this.page);
    }


    @Test(priority = 3)
    @Description("个人任务中心-新建项目任务")
    public void addProjectTask() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("新建任务")).click();
        page.locator("ul.el-dropdown-menu li.el-dropdown-menu__item:has-text('项目任务')").click();

        page.getByPlaceholder("请输入").first().fill(projectTaskName);
        page.getByPlaceholder("选择人员").first().click();
        page.locator("div").filter(new Locator.FilterOptions().setHasText(Pattern.compile("^杨徐陈\\/028845$"))).first().click();
        //选人员的确定按钮
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确 定")).nth(1).click();
        //提交任务的确定按钮
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确 定")).nth(0).click();
        assertThat(page.locator("div[role='alert'] >> text=新建成功")).isVisible();

    }

    @Test(priority = 4)
    @Description("个人任务中心-处理项目任务-进行中")
    public void handleProjectTask() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.locator(".el-table__fixed-body-wrapper > .el-table__body > tbody > tr > .el-table_1_column_12 > .cell > button").first().click();
        page.locator("span").filter(new Locator.FilterOptions().setHasText("任务状态 待启动阶段待开始进行阶段进行中挂起中完成阶段已取消已完成")).locator("i").nth(1).click();
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("进行中")).click();
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确 定")).nth(0).click();
        assertThat(page.locator("div[role='alert'] >> text=更新成功")).isVisible();
        assertThat(page.locator(".el-tag").first()).containsText("进行中");

    }


    @Test(priority = 5)
    @Description("个人任务中心-编辑项目任务-待开始")
    public void editProjectTask() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.locator(".el-table__fixed-body-wrapper > .el-table__body > tbody > tr > .el-table_1_column_12 > .cell > button:nth-child(2)").first().click();
        assertThat(page.getByLabel("任务", new Page.GetByLabelOptions().setExact(true))).containsText("任务");
        page.locator("span").filter(new Locator.FilterOptions().setHasText("任务状态 待启动阶段待开始进行阶段进行中挂起中完成阶段已取消已完成")).locator("i").nth(1).click();
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("待开始")).click();
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确 定")).nth(0).click();
        assertThat(page.locator("div[role='alert'] >> text=更新成功")).isVisible();
        assertThat(page.locator(".el-tag").first()).containsText("待开始");

    }


    @Test(priority = 6)
    @Description("个人任务中心-查看项目任务详情")
    public void projectTaskDetails() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.locator(".el-table__fixed-body-wrapper > .el-table__body > tbody > tr > .el-table_1_column_12 > .cell > button:nth-child(4)").first().click();
        assertThat(page.getByLabel("任务详情")).containsText("任务详情");
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("取 消")).click();

    }

    @Test(priority = 7)
    @Description("个人任务中心-项目任务报工")
    public void projectTaskReporting() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.locator(".el-table__fixed-body-wrapper > .el-table__body > tbody > tr > .el-table_1_column_12 > .cell > button:nth-child(5)").first().click();
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName(" 添加报工")).click();
        page.getByRole(AriaRole.CELL, new Page.GetByRoleOptions().setName(" ")).getByRole(AriaRole.SPINBUTTON).click();
        page.getByRole(AriaRole.CELL, new Page.GetByRoleOptions().setName(" ")).getByRole(AriaRole.SPINBUTTON).fill("2");
        page.getByPlaceholder("请输入").first().click();
        page.getByPlaceholder("请输入").first().fill("任务报工内容");
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("确 定")).click();

    }


    @Test(priority = 8)
    @Description("个人任务中心-项目任务进展")
    public void projectTaskProgress() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.locator(".el-table__fixed-body-wrapper > .el-table__body > tbody > tr > .el-table_1_column_12 > .cell > button:nth-child(6)").first().click();
        page.locator(".content-input").click();
        page.locator(".content-input").fill("项目任务进展");
        page.getByText("进度", new Page.GetByTextOptions().setExact(true)).click();
        //
        page.locator("xpath=//div[@class='el-progress__text' and text()='0%']").click();
        page.getByLabel("slider between 0 and").click();
        page.locator("div").filter(new Locator.FilterOptions().setHasText(Pattern.compile("^信心指数评分\\(0-10\\):$"))).getByRole(AriaRole.SPINBUTTON).click();
        page.locator("div").filter(new Locator.FilterOptions().setHasText(Pattern.compile("^信心指数评分\\(0-10\\):$"))).getByRole(AriaRole.SPINBUTTON).fill("8");
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("保存")).click();
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("发布")).click();

    }


    @Test(priority = 9)
    @Description("个人任务中心-项目任务评论")
    public void projectTaskComment() {
        page.navigate("http://192.168.7.136:3000/extend/task/center");
        page.locator(".el-table__fixed-body-wrapper > .el-table__body > tbody > tr:nth-child(1) > .el-table_1_column_12 > .cell > button:nth-child(7)").click();
        page.locator(".content-input").click();
        page.locator(".content-input").fill("测试评论");
        page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("发送")).click();
        page.locator(".top-right > .el-dialog__close").click();

    }




}
